Page({
    data: {
        recommendationList: [],
        page: 1,
        limit: 20,
        hasMore: true
    },

    onLoad() {
        this.getRecommendationList();
    },

    getRecommendationList() {
        const {
            page,
            limit,
            recommendationList
        } = this.data;
        wx.request({
            url: 'http://localhost:8080/recommendation/list',
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded' // POST请求且是form表单类型
            },
            data: {
                page: page,
                limit: limit
            },
            success: (res) => {
                if (res.data && res.data.data) {
                    // const newList = res.data.data;
                    let newList = res.data.data.records.map((item, index) => {
                        // 根据index设置背景class
                        if (index === 0) item.bgClass = 'gold-bg';
                        else if (index === 1) item.bgClass = 'silver-bg';
                        else if (index === 2) item.bgClass = 'bronze-bg';
                        else item.bgClass = '';
                        return item;
                      });
                    this.setData({
                        recommendationList: recommendationList.concat(newList),
                        hasMore: newList.length === limit // 如果返回数量小于limit，就没有更多了
                    });
                }
            },
            fail: (err) => {
                console.error('请求失败:', err);
            }
        });
    },

    getRecommendationMore() {
        const {
            page,
            limit,
            recommendationList
        } = this.data;
        wx.request({
            url: 'http://localhost:8080/recommendation/list',
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded' // POST请求且是form表单类型
            },
            data: {
                page: page,
                limit: limit
            },
            success: (res) => {
                if (res.data && res.data.data) {
                    // const newList = res.data.data;
                    let newList = res.data.data.records.map((item, index) => {
                        return item;
                      });
                    this.setData({
                        recommendationList: recommendationList.concat(newList),
                        hasMore: newList.length === limit // 如果返回数量小于limit，就没有更多了
                    });
                }
            },
            fail: (err) => {
                console.error('请求失败:', err);
            }
        });
    },

    loadMore() {
        if (!this.data.hasMore) {
            return;
        }
        this.setData({
            page: this.data.page + 1
        }, () => {
            this.getRecommendationMore();
        });
    },

    getBgColorClass(index) {
        console.log("调用了")
        if (index === 0) return 'gold-bg';
        if (index === 1) return 'silver-bg';
        if (index === 2) return 'bronze-bg';
        return '';
    },

     /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.loadMore();
    },
});